#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <strings.h>
#include <errno.h>
#include "charsetconvert.h"
#define CHARSET_MAX 1024

void WriteFile( const char * filename, const char* buf, int iSize);

int main(int argc, char **argv)
{
    int i = 0, fd = 0;
    struct stat st;
    char *s = NULL, charset[CHARSET_MAX];
    CCharSetConvert cs;
    int iRes = 0; 
    int oRet = 0;


    if(argc < 2)
    {
        fprintf(stderr, "Usage:%s file ...\n", argv[0]);
        _exit(-1);
    }

    //test ascii string charset
    int iALen = strlen( argv[1] ) ;
    char * aCharset = cs.GetCharset(argv[1], iALen, oRet );
    if( aCharset!=NULL )
    {
        printf( "get charset:[%s]\n", aCharset );
    }
    else
    {
        printf( "get charset fail.\n" );
    }

    /* test get charset */
    if((fd = open(argv[1], O_RDONLY, 0644)) > 0)
    {
	if(fstat(fd, &st) == 0 && st.st_size > 0 
		&& (s = (char *)calloc(1, st.st_size + 1)))
	{
		unsigned int o_size = st.st_size*3 + 1;
		char * o_buf = (char*)malloc(o_size);
	    if( read(fd, s, st.st_size) > 0 )
	    {
		char * dCharset = cs.GetCharset(s, st.st_size, oRet );
		if( dCharset!=NULL )
		{
			printf( "get charset:[%s]\n", dCharset );
		}
		else
		{
			printf( "get charset fail.\n" );
		}
	    }
	    free(s);
	    s = NULL;
	}
	close(fd);
    }

    /* test auto convert charset to destination charset, default UTF-8 */
    if((fd = open(argv[1], O_RDONLY, 0644)) > 0)
    {
	if(fstat(fd, &st) == 0 && st.st_size > 0 
		&& (s = (char *)calloc(1, st.st_size + 1)))
	{
		unsigned int o_size = st.st_size*3 + 1;
		char * o_buf = (char*)malloc(o_size);
	    if(read(fd, s, st.st_size) > 0 
		&& (iRes=cs.CharsetConvert(s, st.st_size, o_buf, o_size /*, "UTF-8"*/ )) > 0)
	    {
		//fprintf(stdout, "charset of %s:%s\n", argv[i], charset);
		WriteFile( argv[1], o_buf, iRes );
	    }
	    free(s);
	    s = NULL;
	}
	close(fd);
    }
}

void WriteFile( const char * filename, const char* buf, int iSize)
{
	int fd = 0;
	char dstFileName[256] = {0};
	sprintf( dstFileName, "%s-UTF-8", filename );
	printf( "gen filename:%s\n", dstFileName );
	if((fd = open(dstFileName, O_RDWR|O_CREAT, 0644)) > 0)
	{
		int iRet = write(fd, buf, iSize);
		if( iRet>=0 )
		{
			printf("write file ok [%d].\n", iRet);
		}
		else
		{
			printf("write file fail.[%s]\n", strerror(errno) );
		}
		close(fd);
	}
	else{
		printf("open file fail.[%s]\n", strerror(errno) );
		return;
	}
}

/*
void ConvertWrite(char*src, unsigned int in_size, const char *charset)
{
	iconv_t iconv_h;
	char * o_buf = NULL;
	if( strcmp("", charset)==0 )
	{
		printf("ascii codec.\n");
		return;
	}

	if( 0!=strcmp("UTF-8", charset) )
	{
		iconv_h = iconv_open("UTF-8", charset);
		if( iconv_h<0 )
		{
			printf( "iconv create fail[%s].\n", strerror(errno) );
			return;
		}
		size_t iLen = in_size;
		size_t oLen = in_size*4+1;
		size_t oLenTrue = oLen;
		o_buf = (char*)malloc(oLen);
		char * oPtr = o_buf;
		memset( o_buf, 0, oLen );
		printf( "iLen:%u, oLen:%u, isrc:%8x, odst:%8x\n", iLen, oLenTrue, src, oPtr);
		int iRet = iconv( iconv_h, &src, (size_t*)&iLen, &oPtr, (size_t*)&oLenTrue);
		if( iRet<0 )
		{
			printf( "iconv convert fail[%s].\n", strerror(errno) );
			printf( "-------------------------------------------\n" );
			printf( "%s\n", o_buf );
			printf( "-------------------------------------------\n" );
			//return;
		}

		printf( "iLen:%u, oLen:%u, isrc:%8x, odst:%8x\n", iLen, oLenTrue, src, oPtr);
		int fd = 0;
		//printf("iconv result:[%d]%s\n", iRet, o_buf);
		if((fd = open("avenresult", O_RDWR|O_CREAT, 0755)) > 0)
		{
			iRet = write(fd, o_buf, oLen-oLenTrue);
			if( iRet>=0 )
			{
				printf("write file ok [%d].\n", iRet);
			}
			else
			{
				printf("write file fail.[%s]\n", strerror(errno) );
			}
			close(fd);
		}
		else{
			printf("open file fail.[%s]\n", strerror(errno) );
			return;
		}
	}
}
*/
